﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace exsportance.Silverlight.Common.Helper
{
    /// <summary>
    /// A transition with fade effect
    /// </summary>
    public class FadeTransition : TransitionBase
    {
        #region members
        private UserControl _newPage;
        private UserControl _oldPage;
        private TimeSpan _time;
        #endregion

        #region constructor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="duration"></param>
        public FadeTransition(TimeSpan duration)
        {
            _time = duration;
        }

        /// <summary>
        /// Default constructor. 2 seconds fade effect
        /// </summary>
        public FadeTransition()
            : this(TimeSpan.FromSeconds(2))
        {
        }
        #endregion

        #region public mehtods
        public override void PerformTranstition(UserControl newPage, UserControl oldPage)
        {
            _newPage = newPage;
            _oldPage = oldPage;

            Duration duration = new Duration(_time);

            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = duration;
            animation.To = 0;

            Storyboard sb = new Storyboard();
            sb.Duration = duration;
            sb.Children.Add(animation);
            sb.Completed += (s, e) => { OnTransitionCompleted(_newPage, _oldPage); };

            Storyboard.SetTarget(animation, oldPage);
            Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));

            sb.Begin();
        }
        #endregion
    }

}
